package com.hzdesk.media.koreabatchmatvector.adapter;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.bean.CloudListBean;
import com.hzdesk.media.koreabatchmatvector.config.RBConfig;
import com.hzdesk.media.koreabatchmatvector.inter.ICloudListListenter;
import com.hzdesk.media.koreabatchmatvector.utils.FileNameUtils;
import com.hzdesk.media.koreabatchmatvector.utils.FormatDateUtils;

import java.util.List;
import java.util.logging.Handler;

/**
 * Created by Administrator on 2018/6/28.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class CloudListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {
    private ICloudListListenter mICloudListListenter = null;
    private List<CloudListBean.RowsBean> mlist = null;
    private Context tContext;
    private int NORMAL_TYPE_VIEW = 0;
    private int FOOT_TYPE_VIEW = 1;


    static final int LOADING_MORE = 1;                                                              //正在加载更多
    static final int NO_MORE = 2;                                                                   //没有更多
    int footer_state = 1;                                                                           //脚布局当前的状态,默认为没有更多

    public CloudListAdapter(Context mcontext) {
        tContext = mcontext;
    }

    public void setmICloudListListenter(ICloudListListenter mICloudListListenter) {
        this.mICloudListListenter = mICloudListListenter;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == NORMAL_TYPE_VIEW){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cloud_list_item,parent,false);
            CloudListHodler mCloudListHodler = new CloudListHodler(view);
            return mCloudListHodler;
        }else if(viewType == FOOT_TYPE_VIEW){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cloud_list_foot_item,parent,false);
            FootViewHodler footViewHodler =    new FootViewHodler(view);
            return footViewHodler;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
            if(holder instanceof CloudListHodler){
                final CloudListHodler cloudListHodler = (CloudListHodler) holder;
                normalDateShow(position, cloudListHodler);
                cloudListHodler.iv_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(mICloudListListenter != null){
                            mICloudListListenter.delectFile(position);
                        }
                    }
                });
                cloudListHodler.iv_dowmload.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(mICloudListListenter != null){
                            mICloudListListenter.downloadFile(position);
                        }
                    }
                });
            } else if(holder instanceof FootViewHodler){
                FootViewHodler footViewHodler = (FootViewHodler) holder;
                footShow(position, footViewHodler);

            }

    }

    public void updateDate(List<CloudListBean.RowsBean> list){
        mlist = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mlist != null?mlist.size()+1:0 ;
    }
    @Override
    public int getItemViewType(int position) {
        //如果position加1正好等于所有item的总和,说明是最后一个item,将它设置为脚布局
        if (position + 1 == getItemCount()) {
            return FOOT_TYPE_VIEW;
        } else {
            return NORMAL_TYPE_VIEW;
        }
    }
    /**
     * 改变脚布局的状态的方法,在activity根据请求数据的状态来改变这个状态
     *
     * @param state
     */
    public void changeState(int state) {
        this.footer_state = state;
        notifyDataSetChanged();

    }

    // 展示正常数据
    private void normalDateShow(int position, CloudListHodler cloudListHodler) {
        if(mlist != null){
            CloudListBean.RowsBean mRowsBean =  mlist.get(position);
            Object MpicUrl =mRowsBean.picUrl;
            if(MpicUrl != null){
                Glide.with(tContext)
                        .load(RBConfig.HOST_IP+(String) MpicUrl)
                        .placeholder(R.drawable.video_list_image)//图片加载出来前，显示的图片
                        .error(R.drawable.video_list_image)//图片加载失败后，显示的图片
                        .into(cloudListHodler.first_picture);
            }
            String fileUrl = mRowsBean.fileUrl;
            if(fileUrl != null){
                String name =  FileNameUtils.getFileName(fileUrl);
                cloudListHodler.tv_name.setText(mRowsBean.getName()+".mp4");
            }
            long createTime =  mRowsBean.createTime;
            cloudListHodler.tv_time.setText(FormatDateUtils.getCreateDate(createTime));
            int progress = mlist.get(position).progress;
            cloudListHodler.item_pg.setProgress(mlist.get(position).progress);
            if(position == 0){
                cloudListHodler.item_lin.setBackground(tContext.getResources().getDrawable(R.drawable.mouse_list_top_bg));
            }
            if(position == (mlist.size()-1)){
                cloudListHodler.item_lin.setBackground(tContext.getResources().getDrawable(R.drawable.mouse_list_bootom_bg));
            }
        }
    }

    // 展示footView 的信息
    private void footShow(int position, FootViewHodler footViewHodler) {
        if(position == 0){
            footViewHodler.item_rl.setVisibility(View.GONE);
        }
        switch (footer_state){
            case LOADING_MORE:
                footViewHodler.item_rl.setVisibility(View.VISIBLE);
                footViewHodler.item_pg.setVisibility(View.VISIBLE);
                footViewHodler.tv_loding.setVisibility(View.VISIBLE);
                footViewHodler.tv_loding.setText(R.string.is_loading);
                break;
            case NO_MORE:
                footViewHodler.item_rl.setVisibility(View.VISIBLE);
                footViewHodler.item_pg.setVisibility(View.GONE);
                footViewHodler.tv_loding.setVisibility(View.VISIBLE);
                footViewHodler.tv_loding.setText(R.string.there_is_bo_data);
                break;
        }
    }
    class CloudListHodler extends RecyclerView.ViewHolder{
        private ImageView first_picture;
        private TextView tv_name;
        private TextView tv_time;
        private ImageView iv_delete;
        private ImageView iv_dowmload;
        private ProgressBar item_pg;
        private LinearLayout item_lin;
        public CloudListHodler(View itemView) {
            super(itemView);
            first_picture =   itemView.findViewById(R.id.cloud_list_item_iv_first_picture);
            tv_name =   itemView.findViewById(R.id.cloud_liet_item_tv_name);
            tv_time =   itemView.findViewById(R.id.cloud_liet_item_tv_time);
            iv_delete =   itemView.findViewById(R.id.cloud_liet_item_iv_delete);
            iv_dowmload =   itemView.findViewById(R.id.cloud_liet_item_iv_dowmload);
            item_pg =   itemView.findViewById(R.id.cloud_list_item_pg);
            item_lin =   itemView.findViewById(R.id.cloud_liet_item_lin);
        }
    }
    class FootViewHodler extends RecyclerView.ViewHolder{

        private final TextView tv_loding;
        private final ProgressBar item_pg;
        private final RelativeLayout item_rl;

        public FootViewHodler(View itemView) {
            super(itemView);
            tv_loding = itemView.findViewById(R.id.cloud_list_foot_item_tv_loding);
            item_pg = itemView.findViewById(R.id.cloud_list_foot_item_pg);
            item_rl = itemView.findViewById(R.id.cloud_list_foot_item_rl);
        }
    }
}
